<?php

namespace App\Http\Controllers\api;

use App\ActivityBargain;
use App\ActivityBargainInitiate;
use App\ActivityBargainJoin;
use App\ActivityGroup;
use App\ActivityGroupAdd;
use App\ActivityGroupPrice;
use App\ActivityGroupRefund;
use App\Exceptions\OrderException;
use App\Files;
use App\Goods;
use App\GoodsBanner;
use App\GoodsImg;
use App\GoodSpu;
use App\GoodsSnapshot;
use App\GoodsSpu;
use App\Http\Services\FileManager;
use App\Http\Services\ToolsManager;
use App\Http\Services\UserManager;
use App\Http\Services\Wechat\WechatLogin;
use App\Images;
use App\OrderItem;
use App\Orders;
use App\User;
use App\UserDaka;
use App\UserInfo;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener;

class ActivityController extends Controller
{
    /**
     * 获取拼团活动列表接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivityGroupList(Request $request){
        /** @var ActivityGroup $activityGroup */
        $activityGroupList = ActivityGroup::whereStatus(1)->where('end_time','>',date("Y-m-d H:i:s",time()))->orderBy('created_at','desc')->get();
        if(count($activityGroupList) > 0){
            foreach($activityGroupList as $activityGroup){
                /** @var Goods $thisGoods */
                $thisGoods = Goods::whereType('group')->whereIsShop(1)->whereId($activityGroup->goods_id)->first();
                if($thisGoods){
                    $thisGoods->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);

                    /** @var GoodsSpu $goodsSpu */
                    $goodsSpu = GoodsSpu::whereGoodsId($activityGroup->goods_id)->orderBy('sort','asc')->first();
                    if($goodsSpu){
                        /** @var ActivityGroupPrice $activityGroupPrice */
                        $activityGroupPrice = ActivityGroupPrice::whereGoodsId($activityGroup->goods_id)->whereGoodsSpuId($goodsSpu->id)->first();
                        if($activityGroupPrice){
                            $thisGoods->s_price   = $activityGroupPrice->many_price;
                            $thisGoods->stock     = $activityGroupPrice->stock;
                        }else{
                            $thisGoods->s_price   = $goodsSpu->price;
                            $thisGoods->stock     = $goodsSpu->stock;
                        }
                    }

                    $activityGroup->goods_info = $thisGoods;
                }else{
                    $activityGroup->goods_info = null;
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $activityGroupList]);
    }

    /**
     * 获取活动详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivityGroupDetail(Request $request){
        $activityGroupId = !is_null($request->get('activity_group_id')) ? $request->get('activity_group_id') : 0;

        /** @var ActivityGroup $thisActivityGroup */
        $thisActivityGroup = ActivityGroup::whereId($activityGroupId)->whereStatus(1)->first();
        if(!$thisActivityGroup)
            return response()->json(['level' => 'error','message' => '该活动已下架']);

        /** @var Goods $goodsDetails */
        $goodsDetails = Goods::whereId($thisActivityGroup->goods_id)->whereType('group')->whereIsShop(1)->first();
        if($goodsDetails){
            $goodsDetails->thumb_url = FileManager::getImgUrl($goodsDetails->thumb_id);

            $goodsDetails->detail = str_replace("/uploads/ueditor/php/upload/image",env('HOST_URL')."/uploads/ueditor/php/upload/image",$goodsDetails->detail);

            /** @var GoodsBanner $goodsBanner */
            $goodsBanners = GoodsBanner::whereGoodsId($goodsDetails->id)->orderBy('sort','asc')->orderBy('created_at','desc')->get();
            $goodsBannerUrls = array();
            if(count($goodsBanners) > 0){
                foreach($goodsBanners as $goodsBanner){
                    $goodsBannerUrl = FileManager::getImgUrl($goodsBanner->img_id);
                    array_push($goodsBannerUrls,$goodsBannerUrl);
                }
            }

            $goodsDetails->goods_banner = $goodsBannerUrls;

            /** @var GoodsSpu $goodSpu */
            $goodSpuList = GoodsSpu::whereGoodsId($goodsDetails->id)->whereStatus(1)->orderBy('sort','asc')->get();
            $groupArr = array();
            if(count($goodSpuList) > 0){
                foreach($goodSpuList as $goodSpu){
                    /** @var ActivityGroupPrice $activityGroup */
                    $activityGroup = ActivityGroupPrice::whereActivityGroupId($activityGroupId)->whereGoodsId($goodsDetails->id)->whereGoodsSpuId($goodSpu->id)->first();
                    if($activityGroup) {
                        $goodSpu->many_price = $activityGroup->many_price;
                        array_push($groupArr,$activityGroup->many_price);
                    }else{
                        $goodSpu->many_price = $goodSpu->price;
                        array_push($groupArr,$goodSpu->price);
                    }
                }
            }

            $goodSpuPrice = GoodsSpu::whereGoodsId($goodsDetails->id)->whereStatus(1)->pluck('price');
            //单独购买最低价
            $goodsDetails->alone_price = min($goodSpuPrice->toArray());
            //团购最低价
            $goodsDetails->group_price = min($groupArr);

            $goodsDetails->goods_spu_info            = $goodSpuList;
            $thisActivityGroup->goods_info            = $goodsDetails;
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $thisActivityGroup]);
    }

    /**
     * 获取正在进行拼单的列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getInitiateGroupList(Request $request){
        $activityGroupId = !is_null($request->get('activity_group_id')) ? $request->get('activity_group_id') : 0;

        /** @var ActivityGroup $thisActivityGroup */
        $thisActivityGroup = ActivityGroup::whereId($activityGroupId)->whereStatus(1)->first();
        if(!$thisActivityGroup)
            return response()->json(['level' => 'error','message' => '活动已下架']);

        if(strtotime($thisActivityGroup->end_time) <= time())
            return response()->json(['level' => 'error','message' => '该活动已过期']);

        /** @var ActivityGroupAdd $activityGroupOwner */
        $activityGroupOwnerList = ActivityGroupAdd::whereAgId($thisActivityGroup->id)->whereGroupId(0)->where('end_time','>',date('Y-m-d H:i:s',time()))->whereStatus(0)->get();
        if(count($activityGroupOwnerList) > 0){
            foreach($activityGroupOwnerList as $activityGroupOwner){
                /** @var UserInfo $ownerUserInfo */
                $ownerUserInfo = UserInfo::whereId($activityGroupOwner->user_id)->first();
                if($ownerUserInfo){
                    $activityGroupOwner->nickname  = $ownerUserInfo->nickname;
                    $activityGroupOwner->photo_url = FileManager::getImgUrl($ownerUserInfo->photo_id);
                }else{
                    $activityGroupOwner->nickname  = null;
                    $activityGroupOwner->photo_url = null;
                }

                /** @var ActivityGroupAdd $activityGroupAdd */
                $activityGroupAddList = ActivityGroupAdd::whereGroupId($activityGroupOwner->id)->whereStatus(0)->get();
                if(count($activityGroupAddList) > 0){
                    foreach($activityGroupAddList as $activityGroupAdd){
                        /** @var UserInfo $addUserInfo */
                        $addUserInfo = UserInfo::whereId($activityGroupAdd->user_id)->first();
                        if($addUserInfo){
                            $activityGroupAdd->nickname  = $addUserInfo->nickname;
                            $activityGroupAdd->photo_url = FileManager::getImgUrl($addUserInfo->photo_id);
                        }else{
                            $activityGroupAdd->nickname  = null;
                            $activityGroupAdd->photo_url = null;
                        }
                    }
                }

                $activityGroupOwner->group_last_num = $thisActivityGroup->max_group_num - $activityGroupOwner->group_num;
                $activityGroupOwner->group_add_list = $activityGroupAddList;
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $activityGroupOwnerList]);
    }

    /**
     * 获取我的拼团动态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserGroupStatus(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if($user){
            $endActivityGroupIds = ActivityGroup::whereStatus(1)->where('end_time','>',date('Y-m-d H:i:s',time()))->pluck('id');

            /** @var ActivityGroupAdd $activityGroupAdd */
            $activityGroupAddList = ActivityGroupAdd::whereIn('ag_id',$endActivityGroupIds)->whereUserId($user->id)->whereStatus(0)->where('end_time','>',date('Y-m-d H:i:s',time()))->orderBy('created_at','desc')->get();
            if(count($activityGroupAddList) > 0){
                foreach($activityGroupAddList as $activityGroupAdd){
                    $activityGroup = ActivityGroup::whereId($activityGroupAdd->ag_id)->whereStatus(1)->first();
                    if($activityGroup){
                        /** @var Goods $thisGoods */
                        $thisGoods = Goods::whereType('group')->whereIsShop(1)->whereId($activityGroup->goods_id)->first();
                        if($thisGoods){
                            $thisGoods->thumb_url = FileManager::getImgUrl($thisGoods->thumb_id);

                            /** @var GoodsSpu $goodsSpu */
                            $goodsSpu = GoodsSpu::whereGoodsId($activityGroup->goods_id)->orderBy('sort','asc')->first();
                            if($goodsSpu){
                                /** @var ActivityGroupPrice $activityGroupPrice */
                                $activityGroupPrice = ActivityGroupPrice::whereGoodsId($activityGroup->goods_id)->whereGoodsSpuId($goodsSpu->id)->first();
                                if($activityGroupPrice){
                                    $thisGoods->s_price   = $activityGroupPrice->many_price;
                                    $thisGoods->stock     = $activityGroupPrice->stock;
                                }else{
                                    $thisGoods->s_price   = $goodsSpu->price;
                                    $thisGoods->stock     = $goodsSpu->stock;
                                }
                            }

                            $activityGroup->goods_info = $thisGoods;
                        }else{
                            $activityGroup->goods_info = null;
                        }

                        $activityGroupAdd->activity_group_info = $activityGroup;
                        $leftTime = strtotime($activityGroupAdd->end_time) - time();
                        if($activityGroupAdd->status == 0 && $leftTime  > 0 && strtotime($activityGroup->end_time) > time()){
                            $activityGroupAdd->left_time   = $leftTime;
                        }
                    }
                }
            }else{
                $activityGroupAddList = null;
            }
        }else{
            $activityGroupAddList = null;
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $activityGroupAddList]);
    }

    /**
     * 获取拼团团员信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPayGroupAdd(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        $user_id=$user ? $user->id : 0;
//        if(!$user)
//            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $orderNum = $request->get('order_num');  //团主的订单号

        /** @var Orders $thisOrder */
        $thisOrder = Orders::whereOrderNum($orderNum)->whereType('group')->first();
        if(!$thisOrder)
            return response()->json(['level' => 'error','message' => '订单不存在']);

        /** @var OrderItem $thisOrderItem */
        $thisOrderItem = OrderItem::whereOrderId($thisOrder->id)->first();
        if(!$thisOrderItem)
            return response()->json(['level' => 'error','message' => '订单不存在']);

        $result = array();
        /** @var GoodsSnapshot $thisGoodsSnapshot */
        $thisGoodsSnapshot = GoodsSnapshot::whereId($thisOrderItem->goods_snapshot_id)->first();
        if($thisGoodsSnapshot){
            $result['group_goods_name'] = $thisGoodsSnapshot->name;
            $result['group_goods_spu_name'] = $thisGoodsSnapshot->goods_spu_name;
        }else{
            $result['group_goods_name'] = null;
            $result['group_goods_spu_name'] = null;
        }



        /** @var ActivityGroup $thisActivityGroup */
        $thisActivityGroup = ActivityGroup::whereId($thisOrder->activity_id)->whereStatus(1)->first();
        if($thisActivityGroup){
            /** @var Goods $goodsDetails */
            $goodsDetails = Goods::whereId($thisActivityGroup->goods_id)->whereType('group')->whereIsShop(1)->first();
            if($goodsDetails){
                $goodsDetails->thumb_url = FileManager::getImgUrl($goodsDetails->thumb_id);

                /** @var GoodsSpu $goodSpu */
                $goodSpuList = GoodsSpu::whereGoodsId($goodsDetails->id)->whereStatus(1)->orderBy('sort','asc')->get();
                $groupArr = array();
                if(count($goodSpuList) > 0){
                    foreach($goodSpuList as $goodSpu){
                        /** @var ActivityGroupPrice $activityGroup */
                        $activityGroup = ActivityGroupPrice::whereActivityGroupId($thisActivityGroup->id)->whereGoodsId($goodsDetails->id)->whereGoodsSpuId($goodSpu->id)->first();
                        if($activityGroup) {
                            $goodSpu->many_price = $activityGroup->many_price;
                            array_push($groupArr,$activityGroup->many_price);
                        }else{
                            $goodSpu->many_price = $goodSpu->price;
                            array_push($groupArr,$goodSpu->price);
                        }
                    }
                }

                $goodSpuPrice = GoodsSpu::whereGoodsId($goodsDetails->id)->whereStatus(1)->pluck('price');
                //单独购买最低价
                $goodsDetails->alone_price = min($goodSpuPrice->toArray());
                //团购最低价
                $goodsDetails->group_price = min($groupArr);

                $goodsDetails->goods_spu_info            = $goodSpuList;
                $thisActivityGroup->goods_info            = $goodsDetails;
                $result['activity_info'] = $thisActivityGroup;
            }else{
                $thisActivityGroup->goods_info            = null;
                $result['activity_info']                  = null;
            }
        }else{
            $result['activity_info']                  = null;
        }

        if($thisOrder->group_type == 3){
            /** @var ActivityGroupAdd $Group */
            $Group = ActivityGroupAdd::whereId($thisOrder->group_id)->first();
            if($Group){
                $thisOrder = Orders::whereOrderNum($Group->order_num)->whereType('group')->first();
                if(!$thisOrder)
                    return response()->json(['level' => 'error','message' => '团主订单号不存在']);
            }else{
                return response()->json(['level' => 'error','message' => '团主信息不存在']);
            }
        }

        /** @var ActivityGroupAdd $activityGroupAdd */
        $activityGroupAdd = ActivityGroupAdd::whereAgId($thisOrder->activity_id)->whereGroupId(0)->whereOrderNum($thisOrder->order_num)->first();
        if($activityGroupAdd){
            if(strtotime($activityGroupAdd->end_time) <= time()){
                $activityGroupAdd->status = 2;
                $activityGroupAdd->save();
            }

            /** @var UserInfo $thisUserInfo */
            $thisUserInfo = UserInfo::whereUserId($activityGroupAdd->user_id)->first();
            if($thisUserInfo){
                $activityGroupAdd->nickname  = $thisUserInfo->nickname;
                $activityGroupAdd->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
            }else{
                $activityGroupAdd->nickname  = null;
                $activityGroupAdd->photo_url = null;
            }

            /** @var ActivityGroupAdd $activityGroup */
            $activityGroupChild = ActivityGroupAdd::whereGroupId($activityGroupAdd->id)->get();
            if(count($activityGroupChild) > 0){
                foreach($activityGroupChild as $activityGroup){
                    /** @var UserInfo $thisUserInfo */
                    $thisUserInfo = UserInfo::whereUserId($activityGroup->user_id)->first();
                    if($thisUserInfo){
                        $activityGroup->nickname  = $thisUserInfo->nickname;
                        $activityGroup->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
                    }else{
                        $activityGroup->nickname  = null;
                        $activityGroup->photo_url = null;
                    }
                }
            }

            $activityGroupAdd->group_add_list = $activityGroupChild;
            $isActivityChild = ActivityGroupAdd::whereGroupId($activityGroupAdd->id)->whereUserId($user_id)->first();
            if($isActivityChild || ($activityGroupAdd->user_id == $user_id)){
                $result['is_join'] = true;
            }else{
                $result['is_join'] = false;
            }
            $result['group_info'] = $activityGroupAdd;
        }else{
            $result['group_info'] = null;
        }

        if($thisActivityGroup && $activityGroupAdd){
            $result['group_last_num'] = (int)$thisActivityGroup->max_group_num - (int)$activityGroupAdd->group_num;
        }else{
            $result['group_last_num'] = null;
        }

        /** @var UserInfo $thisUserInfo */
        $thisUserInfo = UserInfo::whereUserId($user_id)->first();
        if($thisUserInfo){
            $thisUserInfo->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);
            $result['user_info'] = $thisUserInfo;
        }else{
            $result['user_info'] = null;
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $result]);
    }

    /**
     * 获取分享二维码图片
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\WechatException
     */
    public function generateShareQrCode(Request $request){
//        /** @var User $user */
//        $user = UserManager::isLogin($request);
//        if(!$user)
//            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $groupId = !is_null($request->get('group_id')) ? $request->get('group_id') : 0;

//        /** @var UserInfo $userInfo */
//        $userInfo = UserInfo::whereUserId($user->id)->first();
//        if(!$userInfo)
//            return response()->json(['level' => 'error','message' => '用户信息不存在']);

        /** @var ActivityGroupAdd $thisActivityGroupAdd */
        $thisActivityGroupAdd = ActivityGroupAdd::whereId($groupId)->whereGroupId(0)->whereStatus(0)->first();
        if(!$thisActivityGroupAdd)
            return response()->json(['level' => 'error','message' => '拼团已结束']);

        /** @var ActivityGroup $thisActivityGroup */
        $thisActivityGroup = ActivityGroup::whereId($thisActivityGroupAdd->ag_id)->whereStatus(1)->first();
        if(!$thisActivityGroup)
            return response()->json(['level' => 'error','message' => '活动已下架']);

        if(strtotime($thisActivityGroup->end_time) <= time())
            return response()->json(['level' => 'error','message' => '活动结束了']);

        /** @var Goods $thisGoods */
        $thisGoods = Goods::whereId($thisActivityGroup->goods_id)->whereType('group')->first();
        if(!$thisGoods)
            return response()->json(['level' => 'error','message' => '拼团商品信息不存在']);

        $orders = Orders::whereOrderNum($thisActivityGroupAdd->order_num)->first();
        if(!$orders)
            return response()->json(['level' => 'error','message' => '订单不存在']);

        $orderItem = OrderItem::whereOrderId($orders->id)->first();
        if(!$orderItem)
            return response()->json(['level' => 'error','message' => '订单不存在']);

        /** @var GoodsSpu $thisGoodsSpu */
        $thisGoodsSpu = GoodsSpu::whereId($orderItem->goods_spu_id)->whereStatus(1)->first();
        if(!$thisGoodsSpu)
            return response()->json(['level' => 'error','message' => '商品属性不存在']);

        //背景图片
        $backgroundImgInfo = Image::make(url('images/bj.png'));

        //获取拼团商品图片
        if($thisGoods->thumb_id != 0){
            /** @var Images $thisImage */
            $thisImage = Images::whereId($thisGoods->thumb_id)->first();
            if($thisImage){
                /** @var Files $thisFile */
                $thisFile = Files::whereId($thisImage->file_id)->first();
                if($thisFile){
                    $goodsThumbImgContent = $thisFile->getContent();
                }else{
                    $goodsThumbImgContent = url('images/default_img.png');
                }
            }else{
                $goodsThumbImgContent = url('images/default_img.png');
            }
        }else{
            $goodsThumbImgContent = url('images/default_img.png');
        }

        //拼团商品图
        $goodsThumbImgINfo = Image::make($goodsThumbImgContent);

        //吊证拼团商品图大小
        $goodsThumbImgINfo->resize(642,642);

        //吊证拼团商品图在背景图上的位置
        $backgroundImgInfo->insert($goodsThumbImgINfo,'top-left',54,108);

        //商品名称
        //商品名称的长度
        $goodsCount = mb_strlen($thisGoods->name);
        if($goodsCount && $goodsCount > 4){
            //截取前四个字
            $goodsNameOne = mb_substr($thisGoods->name,0,4,'utf-8');
            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$goodsNameOne, 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //截取后面的数字
            $goodsNameTwo = mb_substr($thisGoods->name,4,null,'utf-8');

            $goodsNameTwo = mb_strimwidth($goodsNameTwo,0,24,'...','utf-8');
            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text($goodsNameTwo.'】', 65, 815, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

        }else{
            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });

            //调整商品名称在图片上的位置
            $backgroundImgInfo->text('超值！我正在拼【'.$thisGoods->name.'】', 65, 780, function($font) {
                $font->file(base_path()."/public/front/simsun.ttc");
//            $font->file(1);
                $font->size(30);
                $font->color('#000000');//5a5a5a
                $font->align('left');
                $font->valign('top');
                $font->angle(0);
            });
        }

        //商品属性名
        $goodsSpuName = $thisGoodsSpu->spu_name;

        //调整商品属性名在图片上的位置
        $backgroundImgInfo->text($goodsSpuName, 65, 875, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(25);
            $font->color('#000000');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });

        //红色背景
        $redBackgroundImgInfo = Image::make(url('images/rbj.png'));

        //调整红色背景在背景图上的位置
        $backgroundImgInfo->insert($redBackgroundImgInfo,'top-left',65,930);

        //拼团人数
        $GroupAddNum = $thisActivityGroup->max_group_num;

        //调整拼团人数在图片上的位置
        $backgroundImgInfo->text($GroupAddNum.'人团', 83, 940, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(25);
            $font->color('#ffffff');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });

        //获取商品拼团最低价
        /** @var GoodsSpu $goodSpu */
        $goodSpuList = GoodsSpu::whereGoodsId($thisGoods->id)->whereStatus(1)->orderBy('sort','asc')->get();
        $groupArr = array();
        if(count($goodSpuList) > 0){
            foreach($goodSpuList as $goodSpu){
                /** @var ActivityGroupPrice $activityGroupPrice */
                $activityGroupPrice = ActivityGroupPrice::whereActivityGroupId($thisActivityGroup->id)->whereGoodsId($thisGoods->id)->whereGoodsSpuId($goodSpu->id)->first();
                if($activityGroupPrice) {
                    array_push($groupArr,$activityGroupPrice->many_price);
                }else{
                    array_push($groupArr,$goodSpu->price);
                }
            }
        }

        //商品拼团价
        $goodsGroupPrice = min($groupArr);

        //调整商品拼团价在图片上的位置
        $backgroundImgInfo->text('￥'.$goodsGroupPrice, 65, 1001, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(30);
            $font->color('#000000');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });

        $goodSpuPrice = GoodsSpu::whereGoodsId($thisGoods->id)->whereStatus(1)->pluck('price');
        //单独购买最低价
        $GoodsOriginalPrice = min($goodSpuPrice->toArray());

        //调整单独购买最低价在图片上的位置
        $backgroundImgInfo->text('￥'.$GoodsOriginalPrice, 200, 1009, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(18);
            $font->color('#808080');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });

        //灰色的线
        $grayLine = '—————';

        //调整单独购买最低价在图片上的位置
        $backgroundImgInfo->text($grayLine, 190, 1015, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(18);
            $font->color('#808080');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });


        $scene = "order_num=".$thisActivityGroupAdd->order_num;
        $path  = 'pages/assembleDetail/main?'.$scene;      //梓杰给

        //生成微信小程序的二维码图片
        $wxAppQrCodeContent = WechatLogin::getWxQrcodeByToken($path);
        if($wxAppQrCodeContent){
            //获取二维码图片
            $wxAppQrCodeImgINfo = Image::make($wxAppQrCodeContent)->resize(125,125);
            //调整二维码图片在图片上的位置
            $backgroundImgInfo->insert($wxAppQrCodeImgINfo,'top-left',560,810);

        }else{
            return response()->json(['level' => 'error','message' => '二维码生成失败,请联系客服']);
        }

        //长按识别二维码
        $activityTimeInfo = "长按小程序码购买";

        //调整长按识别二维码在图片上的位置
        $backgroundImgInfo->text($activityTimeInfo,543,950, function($font) {
            $font->file(base_path()."/public/front/simsun.ttf");
//            $font->file(1);
            $font->size(20);
            $font->color('#808080');//5a5a5a
            $font->align('left');
            $font->valign('top');
            $font->angle(0);
        });

        //输出图片
        $response = response()->make($backgroundImgInfo->encode('png'));
        $response->header('Content-Type', 'image/png');

        return $response;
    }

//    /**
//     * 申请退款接口
//     * @param Request $request
//     * @return \Illuminate\Http\JsonResponse
//     */
//    public function quitGroup(Request $request){
//        /** @var User $user */
//        $user = UserManager::isLogin($request);
//        if(!$user)
//            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);
//
//        $orderNum      = $request->get('order_num');
//        $reason        = $request->get('reason');
//        $problemDesc   = $request->get('problem_desc');
//
//        /** @var Orders $thisOrder */
//        $thisOrder = Orders::whereOrderNum($orderNum)->whereUserId($user->id)->first();
//        if(!$thisOrder)
//            return response()->json(['level' => 'error','message' => '订单不存在']);
//
//        if($thisOrder->is_pay == 0)
//            return response()->json(['level' => 'error','message' => '订单未支付']);
//
//        if(is_null($reason) || $reason == '')
//            return response()->json(['level' => 'error','message' => '请输入退款原因']);
//
//        if(is_null($problemDesc) || $problemDesc != '')
//            return response()->json(['level' => 'error','message' => '请输入问题描述']);
//
//        $ActivityGroupRefund = PaymentController::Refund($thisOrder,'退出拼团成功，退还拼团金额',$user->id);
//        if($ActivityGroupRefund){
//            return response()->json(['level' => 'success','message' => '退款申请成功','data' => $ActivityGroupRefund]);
//        }else{
//            return response()->json(['level' => 'error','message' => '退款申请失败']);
//        }
//    }

}
